#include "selectfrienditem.h"
#include"selectfriendarea.h"
#include"modle/data.h"
SelectFriendItem::SelectFriendItem(SelectFriendScrollArea*owner,const QString& userid,const QIcon&avatorIcon,const QString& name,bool checked,QWidget *parent )
    :owner(owner),
    userid(userid),
    QWidget(parent),
    isSelected(checked)
{
    layout=new QHBoxLayout();
    layout->setSpacing(12);
    layout->setContentsMargins(0,0,0,0);

    this->setLayout(layout);
    this->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Fixed);
    this->setFixedHeight(60);
    this->setCursor(Qt::PointingHandCursor);


    this->setStyleSheet(
        "SelectFriendItem {"
        "   background: #ffffff;"
        "   border: 1px solid #e9ecef;"
        "   border-radius: 8px;"
        "   margin: 2px;"
        "}"
        "SelectFriendItem:hover {"
        "   background: qlineargradient(x1:0, y1:0, x2:1, y2:0,"
        "               stop:0 rgba(13, 110, 253, 0.08), stop:1 rgba(25, 135, 84, 0.08));"
        "   border: 1px solid rgba(13, 110, 253, 0.3);"
        "}"
        "QLabel {"
        "   color: #495057;"
        "   font-size: 14px;"
        "   font-weight: 500;"
        "   background: transparent;"
        "}"
        );

    checkbox=new QCheckBox();
    checkbox->setFixedSize(24,24);
    checkbox->setChecked(this->isSelected);

    QString checkboxstyle =
        "QCheckBox::indicator {"
        "   width: 20px;"
        "   height: 20px;"
        "   image: url(:/Resources/noselected.png);"
        "}"
        "QCheckBox::indicator:checked {"
        "   image: url(:/Resources/selected.png);"
        "}";

    checkbox->setStyleSheet(checkboxstyle);

    avator=new QPushButton();
    avator->setFixedSize(40,40);
    avator->setIcon(avatorIcon);
    avator->setIconSize(QSize(36,36));
    avator->setStyleSheet(
        "QPushButton {"
        "   border: 2px solid #e9ecef;"
        "   border-radius: 20px;"
        "   background: #ffffff;"
        "}"
        "QPushButton:hover {"
        "   border: 2px solid #0d6efd;"
        "}"
        );

    namelabel=new QLabel();
    namelabel->setText(name);
    namelabel->setStyleSheet(
        "QLabel {"
        "   color: #212529;"
        "   font-size: 14px;"
        "   font-weight: 500;"
        "   background: transparent;"
        "}"
        );

    layout->addWidget(checkbox);
    layout->addWidget(avator);
    layout->addWidget(namelabel);
    layout->addStretch(1);

    //连接信号槽函数
    connect(checkbox,&QCheckBox::toggled,owner,[=](bool checked){
        //拿到owner的父元素的父元素
        QObject*tem=owner->parent()->parent();
        SelectFriendArea*grandfather=nullptr;
        if(tem->isWidgetType())
        {
            grandfather=dynamic_cast<SelectFriendArea*>(tem);
        }
        if(checked)
        {
            if(this->owner==grandfather->getLeftScroll())
            {
                grandfather->addSelectedItem(userid,avatorIcon,name,true);
            }
            else if(this->owner==grandfather->getRightScroll())
            {
                LOG()<<"右侧item点击了";
            }
            else
            {
                LOG()<<"无法得到正确的滚动区域";
            }
        }
        else
        {
            if(this->owner==grandfather->getLeftScroll())
            {
                grandfather->deleteSelectedItem(userid);
                //还要改变左侧滚动滚动区域的状态为没有被选中
            }
            else if(this->owner==grandfather->getRightScroll())
            {
                grandfather->deleteSelectedItem(userid);
            }
            else
            {
                LOG()<<"无法得到正确的滚动区域";
            }
        }
    });
}

const QString SelectFriendItem::userID()
{
    return this->userid;
}

void SelectFriendItem::setChecked(bool check)
{
    this->isSelected=check;
}

QCheckBox *SelectFriendItem::getCheckBox()
{
    return this->checkbox;
}
